<?php

namespace addons\shopro\controller\car;

use addons\shopro\controller\Base;
use app\admin\model\usedcar\Carnewscate;
use app\common\model\car\CarNewsPraiseModel;
use app\common\model\car\CarNewsReviewModel;
use think\Db;
use app\common\library\Ems;
use think\Request;

class Carnews extends Base
{

    protected $noNeedLogin = ['index', 'details','cate_index'];
    protected $noNeedRight = ['*'];
    protected $model;
    protected $cateModel;
    protected $praiseModel;
    protected $reviewModel;
    public function __construct()
    {
        parent::__construct();
        $this->model          = new \app\admin\model\usedcar\Carnews();
        $this->cateModel      = new Carnewscate();
        $this->praiseModel    = new CarNewsPraiseModel();
        $this->reviewModel    = new CarNewsReviewModel();
    }

    /**
     * @menu 新闻分类
     * @Author:男人的坏
     * @Date:2024-05-27 16:44
     */

    public function cate_index(){
        $data = $this->cateModel->where(['status'=>1])->field('id,catename')->order('weigh', 'desc')->select();
        $this->success('ok',$data);
    }

    /**
     * @menu:新闻列表
     * @Author:男人的坏
     * @Date:2024-05-27 16:21
     */
    public function index()
    {
        $query = $this->model->where(['status' => 1]);
        $key   = input('key');
        if ($key) {
            $query->where(['name|title' => ['like', '%'.$key.'%']]);
        }
        $cate_id = input('cate_id');
        if ($cate_id) {
            $query->where(['cate_id' => $cate_id]);
        }

        $data = $query->order('weigh', 'desc')->paginate()->each(function ($query) {
            $query->catename = $this->cateModel->where(['id'=>$query->cate_id])->value('catename');
        });
        $this->success('ok', $data);
    }


    /**
     * @menu:新闻详情
     * @Author:男人的坏
     * @Date:2024-05-27 16:21
     */

    public function details()
    {
        $ids = input('ids');
        if (!$ids) {
            $this->error('缺少参数');
        }
        $info               = $this->model->where(['id' => $ids])->find();
        $reviewData     = $this->reviewModel->with(['user'=>function($query){
            $query->field('id,nickname,avatar');
        }])->where(['car_news_id'=>$ids,'pid'=>0])->paginate();
        $this->success('ok', ['info'=>$info,'reviewData'=>$reviewData]);
    }

    /**
     * @menu 新闻评论
     * @Author:男人的坏
     * @Date:2024-05-27 17:20
     */
    public function add_news_revie(){
        $inser['content']     = input('content');
        $inser['pid']         = input('pid',0);
        $inser['car_news_id'] = input('car_news_id');//文章ID
        if (!$inser['car_news_id'] || !$inser['content']) {
            $this->error('缺少参数');
        }
        $info = $this->model->where(['id' => $inser['car_news_id']])->find();
        if(!$info){
            $this->error('数据不存在，或被删除');
        }
        if($inser['pid']){
            $reviewInfo = $this->reviewModel->where(['id'=>$inser['pid']])->find();
            if(!$reviewInfo){
                $this->error('评论数据不存在，或被删除');
            }
            $reviewInfo->praise_num +=1;
            $reviewInfo->save();
        }
        $inser['user_id'] = $this->auth->id;
        $this->reviewModel->create($inser);
        $info->review +=1;

        $info->save();
        $this->success('ok');
    }

    /**
     * @menu 点赞
     * @Author:男人的坏
     * @Date:2024-05-27 17:30
     */
    public function add_news_praise(){
        $inser['car_news_id'] = input('car_news_id');//文章ID
        $inser['type']        = input('type');
        if (!$inser['car_news_id'] || !in_array($inser['type'], ['news','review'])) {
            $this->error('缺少参数');
        }
        $info = $this->model->where(['id' => $inser['car_news_id']])->find();
        if(!$info){
            $this->error('数据不存在，或被删除');
        }
        $this->praiseModel->create($inser);
        $info->praise +=1;
        $info->save();
        $this->success('ok');
    }
}
